﻿using AspNetCoreRateLimit;
using WomanMotion.WebApi.Wechat.BackgroundServices;
using WomanMotion.WebApi.Wechat.DistributedLock;
using WomanMotion.WebApi.Wechat.HttpClients;
using WomanMotion.WebApi.Wechat.Options;
using WomanMotion.WebApi.Wechat.Repositories;

namespace WomanMotion.WebApi.Api.Wechat
{
    /// <summary>
    /// 微信接口中间件配置
    /// </summary>
    public static class ConfigureWechat
    {
        public static void AddWechat(this IServiceCollection services, IConfiguration conf)
        {
            // 注入配置项（内容见 `appsettings.json` 文件）
            services.Configure<WechatOptions>(conf.GetSection(nameof(WechatOptions)));
            // 注入分布式锁
            services.AddSingleton<IDistributedLockFactory, DistributedLockFactory>();
            // 注入仓储类
            services.AddSingleton<IWechatAccessTokenEntityRepository, WechatAccessTokenEntityRepository>();
            // 注入工厂 HTTP 客户端
            services.AddHttpClient();
            services.AddSingleton<IWechatApiHttpClientFactory, WechatApiHttpClientFactory>();
            // 注入后台任务
            services.AddHostedService<WechatAccessTokenRefreshingBackgroundService>();
        }
    }
}